home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Musique / Quod Libet / quodlibet-3.3.0-installer.exe / bin / uu.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2014-12-31  |  4KB  |  164 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.7)
  3.  
  4. '''Implementation of the UUencode and UUdecode functions.
  5.  
  6. encode(in_file, out_file [,name, mode])
  7. decode(in_file [, out_file, mode])
  8. '''
  9. import binascii
  10. import os
  11. import sys
  12. __all__ = [
  13.     'Error',
  14.     'encode',
  15.     'decode']
  16.  
  17. class Error(Exception):
  18.     pass
  19.  
  20.  
  21. def encode(in_file, out_file, name = None, mode = None):
  22.     '''Uuencode file'''
  23.     opened_files = []
  24.     
  25.     try:
  26.         if in_file == '-':
  27.             in_file = sys.stdin
  28.         elif isinstance(in_file, basestring):
  29.             if name is None:
  30.                 name = os.path.basename(in_file)
  31.             if mode is None:
  32.                 
  33.                 try:
  34.                     mode = os.stat(in_file).st_mode
  35.                 except AttributeError:
  36.                     pass
  37.                 
  38.  
  39.             in_file = open(in_file, 'rb')
  40.             opened_files.append(in_file)
  41.         if out_file == '-':
  42.             out_file = sys.stdout
  43.         elif isinstance(out_file, basestring):
  44.             out_file = open(out_file, 'wb')
  45.             opened_files.append(out_file)
  46.         if name is None:
  47.             name = '-'
  48.         if mode is None:
  49.             mode = 438
  50.         out_file.write('begin %o %s\n' % (mode & 511, name))
  51.         data = in_file.read(45)
  52.         while len(data) > 0:
  53.             out_file.write(binascii.b2a_uu(data))
  54.             data = in_file.read(45)
  55.         out_file.write(' \nend\n')
  56.     finally:
  57.         for f in opened_files:
  58.             f.close()
  59.         
  60.  
  61.  
  62.  
  63. def decode(in_file, out_file = None, mode = None, quiet = 0):
  64.     '''Decode uuencoded file'''
  65.     opened_files = []
  66.     if in_file == '-':
  67.         in_file = sys.stdin
  68.     elif isinstance(in_file, basestring):
  69.         in_file = open(in_file)
  70.         opened_files.append(in_file)
  71.     
  72.     try:
  73.         while True:
  74.             hdr = in_file.readline()
  75.             if not hdr:
  76.                 raise Error('No valid begin line found in input file')
  77.             if not hdr.startswith('begin'):
  78.                 continue
  79.             hdrfields = hdr.split(' ', 2)
  80.             if len(hdrfields) == 3 and hdrfields[0] == 'begin':
  81.                 
  82.                 try:
  83.                     int(hdrfields[1], 8)
  84.                 except ValueError:
  85.                     pass
  86.                 
  87.  
  88.             if out_file is None:
  89.                 out_file = hdrfields[2].rstrip()
  90.                 if os.path.exists(out_file):
  91.                     raise Error('Cannot overwrite existing file: %s' % out_file)
  92.             if mode is None:
  93.                 mode = int(hdrfields[1], 8)
  94.             if out_file == '-':
  95.                 out_file = sys.stdout
  96.             elif isinstance(out_file, basestring):
  97.                 fp = open(out_file, 'wb')
  98.                 
  99.                 try:
  100.                     os.path.chmod(out_file, mode)
  101.                 except AttributeError:
  102.                     pass
  103.  
  104.                 out_file = fp
  105.                 opened_files.append(out_file)
  106.             s = in_file.readline()
  107.             while s and s.strip() != 'end':
  108.                 
  109.                 try:
  110.                     data = binascii.a2b_uu(s)
  111.                 except binascii.Error:
  112.                     v = None
  113.                     nbytes = ((ord(s[0]) - 32 & 63) * 4 + 5) // 3
  114.                     data = binascii.a2b_uu(s[:nbytes])
  115.                     if not quiet:
  116.                         sys.stderr.write('Warning: %s\n' % v)
  117.                     
  118.  
  119.                 out_file.write(data)
  120.                 s = in_file.readline()
  121.             if not s:
  122.                 raise Error('Truncated input file')
  123.         for f in opened_files:
  124.             f.close()
  125.         
  126.         return None
  127.  
  128.  
  129.  
  130. def test():
  131.     '''uuencode/uudecode main program'''
  132.     import optparse as optparse
  133.     parser = optparse.OptionParser(usage = 'usage: %prog [-d] [-t] [input [output]]')
  134.     parser.add_option('-d', '--decode', dest = 'decode', help = 'Decode (instead of encode)?', default = False, action = 'store_true')
  135.     parser.add_option('-t', '--text', dest = 'text', help = 'data is text, encoded format unix-compatible text?', default = False, action = 'store_true')
  136.     (options, args) = parser.parse_args()
  137.     if len(args) > 2:
  138.         parser.error('incorrect number of arguments')
  139.         sys.exit(1)
  140.     input = sys.stdin
  141.     output = sys.stdout
  142.     if len(args) > 0:
  143.         input = args[0]
  144.     if len(args) > 1:
  145.         output = args[1]
  146.     if options.decode:
  147.         if options.text:
  148.             if isinstance(output, basestring):
  149.                 output = open(output, 'w')
  150.             else:
  151.                 print sys.argv[0], ': cannot do -t to stdout'
  152.                 sys.exit(1)
  153.         decode(input, output)
  154.     elif options.text:
  155.         if isinstance(input, basestring):
  156.             input = open(input, 'r')
  157.         else:
  158.             print sys.argv[0], ': cannot do -t from stdin'
  159.             sys.exit(1)
  160.     encode(input, output)
  161.  
  162. if __name__ == '__main__':
  163.     test()
  164.